<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Paypal extends MY_Controller
{
	public function __construct()
	{
        parent::__construct();
        $this->load->model(['site_model','paypal/account_model','paypal/summary_model','paypal/orders_model']);
    }

    public function index()
    {
        $raw=$this->input->raw_input_stream;
        $data=json_decode($raw,true);
        if(!$data["domain"]) die("data error");
        $host = parse_url($data["domain"], PHP_URL_HOST);
        $sites=$this->site_model->getallsync();
        $site=[];
        foreach($sites as $s)
        {
            if(strstr($s["domian"],$host))
            {
                $site=$s;
                break;
            }
        }
        if(!$site) die("site error");
        $exist=$this->orders_model->check_exist($site["id"],$data["orderid"]);
        if(!$exist)
        {
            $accounts=$this->account_model->getacounts($site["id"]);
            if(empty($accounts)) die("account error");
            $rn = array_rand($accounts); 
            $accid=$accounts[$rn]["id"];
            $randaccount=$accounts[$rn]["account"];
            $newdata=[
                "siteid"=>$site["id"],
                "accid"=>$accid,
                "orderid"=>$data["orderid"],
                "amount"=>$data["amount"],
                "products"=>json_encode($data["product"]),
                "curreny"=>$data["currency"],
                "realname"=>$data["realname"],
                "country"=>$data["country"],
                "state"=>$data["state"]["name"],
                "address"=>$data["address"],
                "city"=>$data["city"],
                "phone"=>$data["phone"],
                "postcode"=>$data["postcode"],
                "status"=>1,
                "transid"=>"",
                "ipnstatus"=>"",
                "response"=>""
            ];
            $this->orders_model->create($newdata);
        }
        else
        {
            $account = $this->account_model->getitembyid($exist["accid"]);
            $randaccount=$account["account"];
        }
        echo $randaccount;
        die();
    }

    public function return()
    {
        $data=$_POST;

        if(!$data["domain"]) die("data error");
        $host = parse_url($data["domain"], PHP_URL_HOST);
        $sites=$this->site_model->getallsync();
        $site=[];
        foreach($sites as $s)
        {
            if(strstr($s["domian"],$host))
            {
                $site=$s;
                break;
            }
        }
        if(!$site) die("site error");
        $exist=$this->orders_model->check_exist($site["id"],$data["invoice"]);
        if(!$exist)
        {
            echo "no order found";
            die();
        }
        else
        {
            $newdata=[
                "transid"=>$data["txn_id"],
                "ipnstatus"=>$data["payment_status"],
                "response"=>json_encode($data)
            ];
            if($newdata["ipnstatus"]=="Pending")
            {
                $newdata["status"]=2;
            }
            elseif($newdata["ipnstatus"]=="Completed" && $exist["status"]!=3)
            {
                $newdata["status"]=3;                
                $this->updata_summary($data,$site,$exist["accid"]);                
            }
            $this->orders_model->update($exist["id"],$newdata,$exist["accid"]);
            die("done");
        }
    }

    private function updata_summary($data,$site,$accid)
    {
        $amount=$data["mc_gross"];
        $siteid=$site["id"];
        $this->account_model->summary($accid,$amount);
        $this->summary_model->summary($siteid,$accid,$amount);
    }
}
